<!DOCTYPE html>
<html lang="zh">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.staticfile.net/vue/2.7.8/vue.js"></script>
    <link href="../css/style.css" rel="stylesheet">
</head>

<body>
<div id="app">
    <h1>人生加法</h1>
    <!--  2.add_event触发 handleMessage事件 -->
    <add-method :a="aValue" :b="bValue" @add_event="handleMessage"></add-method>
    <hr>
    <p>result: {{result}}</p>
</div>
<script>
    Vue.component('add-method', {
        props: ['a', 'b'],
        template: `
          <div>
          <button type="button" @click="add">点击</button>
          </div>
        `,
        methods: {
            add: function () {
                let value = this.a + this.b;
                // 1.触发并提交数据（子类触发）
                this.$emit('add_event', value);
            }
        }
    });
    const vm = new Vue({
        el: '#app',
        data: {
            result: 0,
            aValue: Math.round(Math.random() * 10),
            bValue: Math.round(Math.random() * 10)
        },
        methods: {
            // 3. 在父类中执行
            handleMessage: function (msg) {
                this.result = msg;
            }
        }
    });
</script>
</body>
</html>